package com.friends.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.OSSObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.List;

/**
 * Oss操作java工具
 *
 * key生成规则：
 * 主目录+ “/”+ 4位年 + “/”+ 2位月 + “/”+ uuid+”.”+文件后缀
 * 主目录参见com.yourtour.common.constant.AliOssDir的取值
 *
 * Created by xiajiangge on 2017/4/23.
 */
@Component
public class OssTools {

    private OSSClient client;

    @Value("${oss.protocol}")
    private String protocol;
    @Value("${oss.bucketsour}")
    private String bucketsour;
    @Value("${oss.endpoint}")
    private String endpoint;
    @Value("${oss.accessId}")
    private String accessId;
    @Value("${oss.accessKey}")
    private String accessKey;
    @Value("${oss.expireTime}")
    private String expireTime;
    @Value("${oss.filesizeLimit}")
    private String filesizeLimit;
    @Value("${oss.bucketdown}")
    private String bucketdown;


    public OssTools(){

    }

    /**
     * 获取ossclient对象
     * @return OSSClient
     */
    public OSSClient getOssClient(){
        if(client == null) client = new OSSClient(endpoint, accessId, accessKey);
        return client;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getBucketsour() {
        return bucketsour;
    }

    public void setBucketsour(String bucketsour) {
        this.bucketsour = bucketsour;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getAccessId() {
        return accessId;
    }

    public void setAccessId(String accessId) {
        this.accessId = accessId;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public long getExpireTime() {
        return Long.parseLong(expireTime);
    }

    public void setExpireTime(String expireTime) {
        this.expireTime = expireTime;
    }

    public long getFilesizeLimit() {
        return Long.parseLong(filesizeLimit);
    }

    public void setFilesizeLimit(String filesizeLimit) {
        this.filesizeLimit = filesizeLimit;
    }

    public String getBucketdown() {
        return bucketdown;
    }

    public void setBucketdown(String bucketdown) {
        this.bucketdown = bucketdown;
    }

    /**
     * 删除单个ossobject
     * @param key  oss对象key
     */
    public void delOssObj(String key,String bucket){
        if(client == null) client = getOssClient();
        client.deleteObject(bucket, key);
    }

    /**
     * 删除多个ossobject
     * @param keys oss对象key列表
     * @return  未删除的对象key列表
     */
    public List<String> delOssObjs(List<String> keys,String bucket){
        if(client == null) client = getOssClient();
        DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keys).withQuiet(true));
        return deleteObjectsResult.getDeletedObjects();
    }

    /**
     * 拷贝oss对象
     * @param srcKey    原ossobjkey
     * @param destKey   目标ossobjkey
     * @return
     */
    public CopyObjectResult copyOssObj(String sourbucket,String srcKey,String destbucket, String destKey){
        if(client == null) client = getOssClient();
        CopyObjectResult result = client.copyObject(sourbucket, srcKey, destbucket, destKey);
        return result;
    }

    /**
     * 上传字符对象到oss
     * @param key      文件key
     * @param content  待上传字符串
     */
    public void putOssObj(String key, String content,String bucket){
        if(client == null) client = getOssClient();
        client.putObject(bucket, key, new ByteArrayInputStream(content.getBytes()));
    }

    /**
     * 上传文件对象到oss
     * @param key
     * @param file
     * @param bucket
     */
    public void putOssObj(String key, File file, String bucket){
        if(client == null) client = getOssClient();
        client.putObject(bucket, key, file);
    }

    /**
     * 获取oss文件对象
     * 参考https://help.aliyun.com/document_detail/32014.html?spm=5176.doc32014.6.657.2ki8yJ
     * @param key
     * @return OSSObject 文件对象
     */
    public OSSObject getOssObj(String key,String bucket){
        if(client == null) client = getOssClient();
        return client.getObject(bucket,key);
    }

    /**
     * 获取Oss主机外网访问地址
     * @return
     */
    public String getOssHost(String bucket){
        return protocol + "://" + bucket + "." + endpoint;
    }

    /**
     * 获取文件完整访问地址
     * @param key  OssIndex中存储的key
     * @return
     */
    public String getFileLink(String key,String bucket){
        return protocol + "://" + bucket + "." + endpoint +"/"+ key;
    }

    /**
     * 获取图片完整访问地址
     * @param key  OssIndex中存储的key
     * @param imgTailor  图片剪裁参数
     * @return
     */
    public String getImgLink(String key, String imgTailor,String bucket){
        String imgLink = protocol + "://" + bucket + "." + endpoint +"/"+ key;
        if(!StringUtils.isEmpty(imgTailor)){
            imgLink = imgLink + "?x-oss-process=" + imgTailor;
        }else{
            imgLink = imgLink + "!hbstyle";
        }
        return imgLink;
    }


}
